package com.service.admin.order.controller;

import com.service.admin.order.controller.rep.OrderProductDetailsRep;
import com.service.admin.order.controller.rep.OrderProductExcelRep;
import com.service.admin.order.controller.rep.OrderProductPageRep;
import com.service.admin.order.service.OrderProductOtherService;
import com.service.admin.upload.utils.ExcelUtils;
import com.service.core.exception.CommonException;
import com.service.core.rest.BaseController;
import com.service.core.rest.response.BaseResponse;
import com.service.core.rest.response.CommonPageRep;
import com.service.core.rest.response.OneResponse;
import com.service.admin.framework.log.RequestLog;
import com.service.admin.order.controller.req.OrderProductExpressReq;
import com.service.admin.order.controller.req.OrderProductPageReq;
import com.service.core.rest.response.PageResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年10月07日</dd>
 * </dl>
 *
 * @author
 */
@Slf4j
@Api(tags = "会员订单接口")
@RestController
@RequestMapping("order-product-other")
public class OrderProductOtherController extends BaseController {

    @Autowired
    private OrderProductOtherService orderProductOtherService;

    @RequestLog("产品订单接口")
    @ApiOperation("分页")
    @PostMapping("page")
    public PageResponse<OrderProductPageRep> page(@ApiParam @RequestBody OrderProductPageReq pageReq) {
        return successQueryPageResponse(orderProductOtherService.pageOrder(pageReq));
    }


    @ApiOperation("产品发货接口")
    @RequestLog("产品发货接口")
    @PostMapping("deliver/{orderId}")
    public BaseResponse deliverGoods(@PathVariable("orderId") Integer orderId, @RequestBody OrderProductExpressReq productExpressReq) {
        productExpressReq.setId(orderId);
        orderProductOtherService.deliverGoods(productExpressReq);
        return successUpdateBaseResponse();
    }

    @ApiOperation("获取订单详情")
    @RequestLog("获取订单详情")
    @PostMapping("detail/{orderId}")
    public OneResponse<OrderProductPageRep> orderDetail(@PathVariable("orderId") Integer orderId) {
        return successQueryOneResponse(orderProductOtherService.getOrderDetail(orderId));
    }

    @RequestLog("产品订单导出接口")
    @ApiOperation("分页查询导出")
    @PostMapping("export")
    public void export(@ApiParam @RequestBody OrderProductPageReq pageReq, HttpServletResponse rep) {
        //模拟从数据库获取需要导出的数据
        CommonPageRep<OrderProductPageRep> orderList = orderProductOtherService.pageOrder(pageReq);
        List<OrderProductExcelRep> excelList = new ArrayList<>();
        for (OrderProductPageRep productOrder : orderList.getData()){
            for (OrderProductDetailsRep orderDetail : productOrder.getDetailsList()){
                OrderProductExcelRep excelRep = new OrderProductExcelRep();
                excelRep.setMemberAccount(productOrder.getMemberAccount());
                excelRep.setConsigneeName(productOrder.getConsigneeName());
                excelRep.setConsigneeAddress(productOrder.getConsigneeAddress());
                excelRep.setConsigneeMobile(productOrder.getConsigneeMobile());
                excelRep.setCreateTime(productOrder.getCreateTime());
                excelRep.setExpressName(productOrder.getExpressName());
                excelRep.setExpressNo(productOrder.getExpressNo());
                excelRep.setOrderNo(productOrder.getOrderNo());
                excelRep.setOrderNum(orderDetail.getOrderNum());
                excelRep.setFreight(orderDetail.getFreight());
                excelRep.setProductName(orderDetail.getProductName());
                excelList.add(excelRep);
            }
        }
        //导出操作
        if(excelList.size() > 0){
            ExcelUtils.exportExcel(excelList, "礼包产品查询导出表", "订单", OrderProductExcelRep.class, "order.xls", rep);
        }else{
            throw new CommonException(404,"没有可导出的数据了！");
        }
    }

}
